package connect;

import java.io.*;
import java.net.Socket;

public class Cliant {
    String ip="192.168.43.92";
    int port=10000;
    public Cliant(){

    }
    public Cliant(String ip, int port){
        this.ip=ip;
        this.port=port;
    }
    public int login(String name, String password) throws IOException {
        Socket s = new Socket(ip,port);
        BufferedWriter os=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        BufferedReader or=new BufferedReader(new InputStreamReader(s.getInputStream()));
        char info;

//        os.write("1");
//        os.flush();
        os.write("1"+name+"%"+password+"\n");
        os.flush();
//        while (flag){
        info=(char)or.read();
        if(info=='1'){
            String hdata;
            hdata=or.readLine();
            s.close();
            return 1;
//                JOptionPane.showMessageDialog(interactDialog, "登陆成功！", "提示", 1);
        }
        if(info=='2'){
//                os.write("1"+name+"%"+password);
//                os.flush();
            s.close();
            return 2;
//                JOptionPane.showMessageDialog(interactDialog, "用户名或密码错误！", "提示", 0);
        }
//        }
        s.close();
        return 0;
    }
    public  int enroll(String name,String password) throws IOException {
        Socket s=new Socket(ip,port);
        BufferedWriter os=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        BufferedReader or=new BufferedReader(new InputStreamReader(s.getInputStream()));
        boolean flag=true;
        char info;

        os.write("2"+name+"%"+password+"\n");
        os.flush();

//        while (flag){
        info=(char)or.read();
        if(info=='1'){
//                JOptionPane.showMessageDialog(interactDialog, "用户名密码已存在！", "提示", 1);
//                interactDialog.dispose();
//                os.write("2"+name+"%"+password);
//                os.flush();
            s.close();
            return 1;
        }
        if(info=='2'){
            s.close();
            return 2;
//                flag=false;
//                System.out.println("已经输入注册的用户名密码，正在进行检测。。。");
//                JOptionPane.showMessageDialog(interactDialog, "注册成功！", "提示", 1);
//                interactDialog.dispose();
//                GameWin.haveLoggedIn = true;
        }
//        }

        s.close();
        return 0;
    }
    public  void set(String name,int data,int chart) throws IOException {
        Socket s=new Socket(ip,port);
        BufferedWriter os=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String dat=Integer.toString(data);

        if(chart==1){
            os.write("3"+name+"%"+dat+"%"+"charts1");
            os.flush();
        }
        if(chart==5){
            os.write("3"+name+"%"+dat+"%"+"charts2");
            os.flush();
        }


        s.close();
    }
    //    public  void get(String name) throws IOException {
//        Socket s=new Socket(ip,10000);
//        BufferedWriter os=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//
//        os.write("4"+name);
//        os.flush();
//
//        s.close();
//    }
    public  void get(String name,int chart) throws IOException {
        Socket s=new Socket(ip,port);
        BufferedWriter os=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        if(chart==1){
            os.write("4"+name+"%"+"charts1\n");
            os.flush();
        }
        if(chart==2){
            os.write("4"+name+"%"+"charts2\n");
            os.flush();
        }

        s.close();
    }
//    public  void get(String name,int chart) throws IOException {
//        Socket s=new Socket(ip,port);
//        BufferedWriter os=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//        BufferedReader or=new BufferedReader(new InputStreamReader(s.getInputStream()));
//
//        if(chart==1){
//            os.write("4"+name+"%"+"charts1");
//            os.flush();
//        }
//        if(chart==2){
//            os.write("4"+name+"%"+"charts2");
//            os.flush();
//        }
//        String[] strings;
//        String z;
//        z=or.readLine();
//        strings=z.split("%");
//
//        s.close();
//    }
}
